package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.dto.AllScriptDTO;
import com.xbongbong.pro.script.pojo.dto.FixFormDTO;
import com.xbongbong.pro.script.pojo.dto.ScriptDTO;
import com.xbongbong.pro.script.pojo.vo.ScriptVO;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.saas.script.service.InitExplainsService;
import com.xbongbong.saas.script.tempenum.TempCustomerManagementEnum;
import com.xbongbong.saas.service.RepairExplainService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 初始化字段解释
 *
 * @author chaoxiong.lei
 * @date 2019/1/15 22:24
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/form/explains")
public class InitExplainsController {

    @Resource
    private InitExplainsService initExplainsService;
    @Resource
    private RepairExplainService repairExplainService;
    @Resource
    private ProBaseConfig proBaseConfig;

    /**
     * 初始化客户模板
     */
    @RequestMapping(value = "/initQuickAndCommon", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initQuickAndCommon(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initQuickAndCommon(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化客户模板
     */
    @RequestMapping(value = "/initPermission", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initPermission(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = initExplainsService.initPermission(baseDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化客户模板
     */
    @RequestMapping(value = "/initCustomerList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initCustomer(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initCustomerList(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 初始化客户模板
     */
    @RequestMapping(value = "/initAll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initAll(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initAll(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化菜单颜色
     */
    @RequestMapping(value = "/initMenuColor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initMenuColor(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initMenuColor(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化客户模板
     */
    @RequestMapping(value = "/initCustomer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initCustomer(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initCustomer(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化跟进提醒
     */
    @RequestMapping(value = "/initPushNotify", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initPushNotify(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initPushNotify(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化退货退款
     */
    @RequestMapping(value = "/initRefund", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initRefund(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initRefund(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 初始化期初库存表单
     */
    @RequestMapping(value = "/initWarehouseOriStock", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initWarehouseOriStock(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initWarehouseOriStock(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 初始化优惠券模板表单
     */
    @RequestMapping(value = "/initMarketingActivityManagement", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initMarketingActivityManagement(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;

        try {
            ScriptVO scriptVO = initExplainsService.initMarketingActivityManagement(scriptDTO);
            response = new XbbResponse<>(scriptVO);
        } catch (XbbException e) {
            response = new XbbResponse<>(e);
        }

        return JSON.toJSONString(response);
    }

    /**
     * 初始化库存产品
     */
    @RequestMapping(value = "/initStockProduct", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initStockProduct(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initStockProduct(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化产品模板
     */
    @RequestMapping(value = "/initProduct", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initProduct(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initProduct(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化出库单模板
     */
    @RequestMapping(value = "/initOutstock", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initOutstock(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initOutstock(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化入库单模板
     */
    @RequestMapping(value = "/initInstock", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initInstock(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initInstock(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化盘点单解释脚本
     *
     * @param scriptDTO 入参
     * @author lcx
     * @since v1.0
     */
    @RequestMapping(value = "/initInventory", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initInventory(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initInventory(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化联系人解释脚本
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author feng.zheng
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initContact", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initContact(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initContact(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化合同解释脚本
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author feng.zheng
     * @date 2019/1/18 14:51
     */
    @RequestMapping(value = "/initContract", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initContract(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initContract(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化销售机会
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author long.rao
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initOpportunity", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initOpportunity(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initOpportunity(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化访客计划
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author long.rao
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initCommunicatePlan", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initCommunicatePlan(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initCommunicatePlan(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化BOM清单
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author long.rao
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initBomBill", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initBomBill(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initBomBill(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化客户跟进记录
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author long.rao
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initCustomerCommunicate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initCustomerCommunicate(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initCustomerCommunicate(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化供应商跟进记录
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author long.rao
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initSupplierCommunicate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initSupplierCommunicate(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initSupplierCommunicate(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化工作报告(日报)
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author long.rao
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initWorkReportDaily", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initWorkReportDaily(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initWorkReportDaily(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化工作报告(周报)
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author long.rao
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initWorkReportWeekly", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initWorkReportWeekly(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initWorkReportWeekly(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化工作报告(月报)
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author long.rao
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initWorkReportMonthly", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initWorkReportMonthly(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initWorkReportMonthly(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化回款解释脚本
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author feng.zheng
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initPayment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initPayment(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initPayment(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化回款单解释脚本
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author feng.zheng
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initPaymentSheet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initPaymentSheet(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initPaymentSheet(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化付款单解释脚本
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author feng.zheng
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initPayPlanSheet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initPayPlanSheet(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initPayPlanSheet(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化付款计划解释脚本
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author feng.zheng
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initPayPlan", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initPayPlan(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initPayPlan(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化发票解释脚本
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author feng.zheng
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initInvoice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initInvoice(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initInvoice(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 初始化生产单解释脚本
     *
     * @param scriptDTO 入参
     * @return java.lang.String
     * @author feng.zheng
     * @date 2019/1/16 14:51
     */
    @RequestMapping(value = "/initProductionOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initProductionOrder(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initProductionOrder(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始供应商模板
     */
    @RequestMapping(value = "/initSupplier", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initSupplier(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initSupplier(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始供应商联系人模板
     */
    @RequestMapping(value = "/initSupplierContact", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initSupplierContact(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initSupplierContact(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);

    }

    /**
     * 初始采购合同模板
     */
    @RequestMapping(value = "/initPurchase", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initPurchase(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initPurchase(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始报价单模板
     */
    @RequestMapping(value = "/initQuotation", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initQuotation(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initQuotation(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化市场活动模板
     * 如果是新增一家公司的解释：
     * {
     *     "appId":19132, //必传
     *     "menuId":19142,//必传
     *     "frontDev":1,
     *     "userId":1,
     *     "corpid":"1",//必传
     *     "platform":"web",
     *     "isAddExplain":1 //必传，而且必须值是  1
     * }
     * 如果是更新一家公司的解释，那么formId是必传的：
     * {
     *     "appId":19132,
     *     "menuId":19142,
     *     "frontDev":1,
     *     "userId":1,
     *     "corpid":"1",
     *     "formId":17119, //必传
     *     "platform":"web",
     *     "isAddExplain":1
     * }
     *
     * 如果是刷新 explain表中 coprid为0 的那条解释
     *
     * {
     *     "appId":0,
     *     "menuId":0,
     *     "frontDev":1,
     *     "userId":1,
     *     "corpid":"0",
     *     "formId":0,
     *     "platform":"web",
     *     "isAddExplain":0
     * }
     *
     *         //isAddExplain 这个参数是用来区分，刷的是普通公司的解释，还是刷模板解释（模板解释指的是  explain 表中 corpid ,formId 都为 0 的那条数据 ）。
     *         //如果只是刷一家公司的市场活动表单解释，那么 corpid ,appId,menuId,isAddExplain 和这几个参数是必传的，而且isAddExplain的值必须为1;
     *         //关于formId:(1)传输了formId,如果该公司下不存在这个formId的解释，那么新增插入一条该formId对应的市场活动解释,如果如存在，那就是更新解释；（2）如果没有传输formId,那么会去查询该menuId在的表单，没有的话就新增
     */
    @RequestMapping(value = "/initMarketActivity", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initMarketActivity(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initMarketActivity(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 初始线索模板
     */
    @RequestMapping(value = "/initClue", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initClue(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initClue(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始采购退货单
     */
    @RequestMapping(value = "/initReturnedPurchase", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initReturnedPurchase(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initReturnedPurchase(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始调拨单
     */
    @RequestMapping(value = "/initTransfer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initTransfer(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initTransfer(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始装配单
     */
    @RequestMapping(value = "/initAssemble", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initAssemble(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initAssemble(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始仓库
     */
    @RequestMapping(value = "/initWarehouse", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initWarehouse(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initWarehouse(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化客户模板
     */
    @RequestMapping(value = "/initAllField", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initAllField(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initAllField(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 初始化红冲回款单
     */
    @RequestMapping(value = "/initRedPaymentSheet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initRedPaymentSheet(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initRedPaymentSheet(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 初始化红冲回款单
     */
    @RequestMapping(value = "/initBadDebt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initinitBadDebt(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initBadDebt(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 初始化工单
     *
     * @param scriptDTO
     * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2019/3/13 11:14
     * @since
     */
    @RequestMapping(value = "/initWorkOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initWorkOrder(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initWorkOrder(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 处理BI需要的产品和业绩分配
     *
     * @param allScriptDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/initBIProductAndPerformance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initBIProductAndPerformance(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initBIProductAndPerformance(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 处理BI需要的产品和业绩分配
     *
     * @param allScriptDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/initNewAll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initNewAll(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) throws XbbException {
        if (!proBaseConfig.getXbbCode().equals(allScriptDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.repairExplain(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/fixForm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fixForm(@RequestBody @Valid FixFormDTO fixFormDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.fixForm(fixFormDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    @RequestMapping(value = "/fixDefaultForm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fixDefaultForm(@RequestBody @Valid FixFormDTO fixFormDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.fixDefaultForm(fixFormDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    @RequestMapping(value = "/fixWorkOrderForm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fixWorkOrderForm(@RequestBody @Valid FixFormDTO fixFormDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.fixWorkOrderForm(fixFormDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 处理表单
     *
     * @param allScriptDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/initForm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initForm(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initForm(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 处理工单表单
     *
     * @param allScriptDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/initWorkOrderForm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initWorkOrderForm(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initWorkOrderForm(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 更新负责人
     *
     * @param allScriptDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/updateOwnerOrCoUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateOwnerOrCoUser(@RequestBody @Valid AllScriptDTO allScriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = repairExplainService.updateOwnerOrCoUser(allScriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    public static void main(String[] args) {
        CustomerManagementEnum.PHONE.name();
        CustomerManagementEnum[] customerManagementEnum = CustomerManagementEnum.values();
        System.out.println(JSONObject.toJSONString(customerManagementEnum[0]));
        initTest(customerManagementEnum,CustomerManagementEnum.class,TempCustomerManagementEnum.class);
    }


    public static <T,V> void initTest(Enum[] mEnum, Class<T> clazz,Class<V> tempClazz) {
        List<FieldAttrEntity> fieldAttrEntityList = new ArrayList<>();
        List<String> fieldAttrList = InitExplainsHelper.getBusinessEnumAttrs(clazz);
        Object[] objs = clazz.getEnumConstants();
        for (Object obj : objs) {
            FieldAttrEntity fieldAttrEntity = InitExplainsHelper.getNewFieldAttrEntity();
            String saasAttr = "";
            try {
                for (String fieldAttr : fieldAttrList) {
                    String attr = fieldAttr.substring(0, 1).toUpperCase() + fieldAttr.substring(1);
                    Object value = clazz.getMethod("get" + attr).invoke(obj);
                    Class<?> returnType = clazz.getMethod("get" + attr).getReturnType();
                    Method method = FieldAttrEntity.class.getMethod("set" + attr, returnType);
                    method.invoke(fieldAttrEntity, value);
                    if (Objects.equals(fieldAttr, "saasAttr")) {
                        saasAttr = value.toString();
                    }
                }
                Method method = tempClazz.getMethod("init" + saasAttr.substring(0, 1).toUpperCase() + saasAttr.substring(1), FieldAttrEntity.class);
                InitExplainsHelper.commonFiledInit(fieldAttrEntity);
                method.invoke(tempClazz, fieldAttrEntity);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                e.printStackTrace();
            }
            fieldAttrEntityList.add(fieldAttrEntity);
        }
    }

    @RequestMapping(value = "/initCompetitor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initCompetitor(@RequestBody @Valid ScriptDTO scriptDTO, BindingResult br) {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScriptVO scriptVO = initExplainsService.initCompetitor(scriptDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}